/*
 * 文件名：BillingDaoImp.java
 * 版权：Copyright by www.huawei.com
 * 描述：
 * 修改人：Administrator
 * 修改时间：2012-4-28
 * 跟踪单号：
 * 修改单号：
 * 修改内容：
 */
package com.chinasoftosg.sicap.scheduling.job;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.JdbcTemplate;

/**
 * 话单导出功能的DAO
 *     话单数据 批量插入
 * 
 * @author guo
 * @2012-4-26
 */

public class BillingDaoImp
{
    
    /***
     * 日志文件
     */
    private Logger logger = Logger.getLogger(this.getClass());
    
    /***
     * Spring 的JdbcTemplate
     */
    private JdbcTemplate jdbcTemplate;
    
    /***
     * 插入的SQL语句
     */
    private String sql = "";
    
    /***
     * 
     * @param sql
     */
    public BillingDaoImp(String sql)
    {
        this.sql = sql;
    }
    
    public BillingDaoImp()
    {
    }
    
    /****
     * 
     * Description: 批量插入话单<br>
     * 1、传入话单列表数据根据Spring的JdbcTemplate批量插入<br>
     * 2、话单数据<br>
     * 
     * @param billings
     * @param types
     * @throws SQLException 
     * @see
     */
    public void insert(List<String[]> billings, String[] types)
            throws SQLException
    {
        final List<String[]> fbillings = billings;
        final String[] ftypes = types;
        logger.info("开始调用批量插入...");
        jdbcTemplate.batchUpdate(sql, new BatchPreparedStatementSetter()
        {
            @Override
            public void setValues(PreparedStatement arg0, int arg1)
                    throws SQLException
            {
                String[] values = fbillings.get(arg1);
                for (int index = 0; index < values.length; index++)
                {
                    String value = values[index];
                    String type = ftypes[index];
                    //logger.info("value=" + value + " type:" + type);
                    try
                    {
                        if ("Date".equals(type))
                        {
                            arg0.setDate(index + 1,
                                    new java.sql.Date(parse(value).getTime()));
                        }
                        else if ("Integer".equals(type))
                        {
                            try
                            {
                                arg0.setInt(index + 1, Integer.parseInt(value));
                            }
                            catch (NumberFormatException e)
                            {
                                e.printStackTrace();
                                logger.warn("Integer转换错误" + e.getMessage());
                            }
                        }
                        else
                        {
                            arg0.setString(index + 1, value);
                        }
                    }
                    catch (SQLException e)
                    {
                        logger.warn("setDate出现异常 " + e.getMessage());
                    }
                }
            }
            
            @Override
            public int getBatchSize()
            {
                return fbillings.size();
            }
        });
        
    }
    
    /***
     * 
     * Description: 根据话单日志表的时间格式的字符串转化成Date<br>
     * @param time
     * @return 
     * @see
     */
    public Date parse(String time)
    {
        SimpleDateFormat dfs = new SimpleDateFormat("yyyy/MM/dd-HH:mm:ss.SSS");
        Date date = new Date();
        try
        {
            date = dfs.parse(time);
        }
        catch (ParseException e)
        {
            logger.warn("时间格式化错误 " + e.getMessage());
        }
        
        return date;
    }
    
    
    
    //get--set method
    
    public String getSql()
    {
        return sql;
    }
    
    public void setSql(String sql)
    {
        this.sql = sql;
    }
    
    public JdbcTemplate getJdbcTemplate()
    {
        return jdbcTemplate;
    }
    
    public void setJdbcTemplate(JdbcTemplate jdbcTemplate)
    {
        this.jdbcTemplate = jdbcTemplate;
    }
    
    public Logger getLogger()
    {
        return logger;
    }
    
    public void setLogger(Logger logger)
    {
        this.logger = logger;
    }
}
